package com.zqwh.xtool.api.offline;

import android.content.Context;

import com.zqwh.xtool.greendao.DaoManager;
import com.zqwh.xtool.greendao.StockDao;
import com.zqwh.xtool.greendao.db.Stock;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class StockResposity {
    private StockDao stockDao;
    private DaoManager daoManager;

    public StockResposity(Context context){
        daoManager = DaoManager.getInstance();
        daoManager.init(context);
        stockDao = daoManager.getDaoSession().getStockDao();
    }

    /**
     * 插入记录
     *
     * @param stock
     * @return
     */
    public boolean insertStock(Stock stock) {
        return stockDao.insertOrReplace(stock) != -1;
    }

    /**
     * 插入多条记录
     * @param stockList
     * @return
     */
    public boolean insertMultStock(final List<Stock> stockList){
        boolean flag = false;
        try {
            daoManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (Stock stock : stockList) {
                        stockDao.insertOrReplace(stock);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询库存
     * @param devId
     * @param hasLent
     * @param hasRFID
     * @return
     */
    public List<Stock> queryStockList(Long devId, boolean hasLent, boolean hasRFID){
        QueryBuilder<Stock> qb = stockDao.queryBuilder();
        qb.where(StockDao.Properties.Valid.eq(true),
                StockDao.Properties.DevId.eq(devId),
                StockDao.Properties.HasLent.eq(hasLent),
                StockDao.Properties.HasRFID.eq(hasRFID));
        return qb.list();
    }

    /**
     * 根据epcList查询库存
     * @param devId
     * @param epcList
     * @return
     */
    public List<Stock> queryStockListByEpcs(Long devId, List<String> epcList){
        QueryBuilder<Stock> qb = stockDao.queryBuilder();
        qb.where(StockDao.Properties.Epc.in(epcList),
                StockDao.Properties.DevId.eq(devId));
        return qb.list();
    }


    /**
     * 根据toolIdList查询库存
     * @param devId
     * @param toolIdList
     * @return
     */
    public List<Stock> queryStockListByToolIds(Long devId, List<String> toolIdList){
        QueryBuilder<Stock> qb = stockDao.queryBuilder();
        qb.where(StockDao.Properties.StockId.in(toolIdList),
                StockDao.Properties.DevId.eq(devId));
        return qb.list();
    }

    /**
     * 根据stockIdList查询库存
     * @param stockIdList
     * @return
     */
    public List<Stock> queryStockListByStockIdIn(List<String> stockIdList){
        QueryBuilder<Stock> qb = stockDao.queryBuilder();
        qb.where(StockDao.Properties.StockId.in(stockIdList));
        return qb.list();
    }

    /**
     * 查询更新过的数据
     * @return
     */
    public List<Stock> queryStockListUpdated(){
        QueryBuilder<Stock> qb = stockDao.queryBuilder();
        qb.where(StockDao.Properties.Updated.eq(true));
        return qb.list();
    }

    /**
     * 删除所有数据
     */
    public void deleteAll(){
        stockDao.deleteAll();
    }
}
